package com.ocom.payconfig.dao;

import com.ocom.common.entity.payconfig.FeeData;
import com.ocom.common.vo.payconfig.FeeDataByPersonResponse;
import com.ocom.common.vo.payconfig.FeeDataListVo;
import com.ocom.common.vo.payconfig.FeeListVo;
import icu.mhb.mybatisplus.plugln.base.mapper.JoinBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 缴费清单 Mapper 接口
 * </p>
 *
 * @author junShu
 * @since 2023-08-18
 */
@Mapper
public interface IDaoFeeData extends JoinBaseMapper<FeeData> {

    @Select({"<script>",
            " SELECT s.id , s.p_id AS pId , p.p_code AS pCode , p.p_name AS pName , p.dept_id AS deptId , a.acc_type_id AS accType, \n" +
                    " s.plan_money AS planMoney , s.data_status AS dataStatus , s.complete_money AS completeMoney, \n" +
                    " s.refund_money AS refundMoney,s.refund_time  , s.fee_time AS completeTime , s.refund_time AS refundTime, \n" +
                    " s.order_no AS orderNo , s.refund_order_no AS refundOrderNo,de.dept_name \n" +
                    " FROM fee_data s \n" +
                    " LEFT JOIN personnel_info p ON p.p_id = s.p_id \n" +
                    " LEFT JOIN dept_info de ON de.dept_id = p.dept_id \n" +
                    " LEFT JOIN xf_account_info a ON a.p_id = s.p_id \n" +
                    " WHERE s.com_id = #{request.comId}\tAND s.item_id = #{request.feeItemId}\n" +
                    "<when test=\"request.dataStatus != 0\" > "+
                    "AND s.data_status = #{request.dataStatus} \n" +
                    "</when> " +
                    "<when test=\"request.deptIdArray!=null and request.deptIdArray.length > 0 \" > "+
                    "AND p.dept_id IN \n" +
                    "<foreach collection='request.deptIdsList' item='item' open='(' separator=',' close='))'> "+
                    " #{item} "+
                    "</foreach>"+
                    "</when> " +
                    "<when test=\"request.deptId!=null and request.deptId != 0\" > "+
                    "AND p.dept_id = #{request.deptId}\n" +
                    "</when> " +
                    "<when test=\"request.accType!=null and request.accType != 0\" > "+
                    "AND a.acc_type_id = #{request.accType}\n" +
                    "</when> " +
                    "<when test=\"request.pCode!=null and request.pCode!=''\" > " +
                    "AND p.p_code = #{request.pCode}\n" +
                    "</when> " +
                    "<when test=\"request.pName!=null and request.pName!=''\" > "+
                    "AND p.p_name like concat('%', #{request.pName}, '%')\n" +
                    "</when> "+
                    "ORDER BY pCode, pId " +
                    "</script>"})
    List<FeeListVo> feeDataList(@Param("request") FeeDataListVo request);

    @Select({"<script>",
            "SELECT p.p_id AS pId, p.dept_id AS deptId\n" +
                    "FROM personnel_info p , xf_account_info a \n" +
                    "WHERE p.com_id = #{comId} AND p.status = 0 \n" +
                    " AND a.p_id = p.p_id AND a.account_state =0 AND a.acc_type_id = #{accType}\n" +
                    "<when test=\"deptIds!=null and deptIds.length > 0 \" > "+
                    " AND p.dept_id IN "+
                    "<foreach collection='deptIds' item='item' open='(' separator=',' close=')'> "+
                    " #{item} "+
                    "</foreach>"+
                    "</when> " +
            "</script>"})
    List<Map<String, Object>> selectByPId(Long comId, int accType, int[] deptIds);

/*    @Select({"<script>",
            "SELECT s.id , s.item_id AS feeItemId , s.data_status AS dataStatus , f.item_name AS itemName ," +
                    "s.plan_money AS planMoney , s.complete_money AS completeMoney , " +
                    "s.refund_money AS refundMoney , s.fee_time AS completeTime , s.refund_time AS refundTime," +
                    "s.order_no AS orderNo , s.refund_order_no AS refundOrderNo, \n" +
                    "f.remark AS itemRemark , f.status AS itemStatus , f.start_date AS startDate , f.stop_date AS stopDate \n" +
                    "FROM fee_data s LEFT JOIN fee_item f ON f.item_id = s.item_id \n" +
                    "WHERE s.com_id = #{comId} AND s.p_id = #{pId}  AND  f. STATUS > 1 \n" +
                    "<when test=\"dataStatus == 1 \" > "+
                    " AND  s.data_status = 1 \n" +
                    "</when> " +
                    "<when test=\"dataStatus == 4 \" > "+
                    " AND s.data_status IN (4,5,-1)\n" +
                    "</when> " +
                    "<when test=\"dataStatus == 5 \" > "+
                    " AND s.data_status IN (5,-1)\n" +
                    "</when> " +
                    "<when test=\"feeItemId !=null and feeItemId > 0\" > "+
                    " AND s.item_id = #{feeItemId}\n" +
                    "</when> " +
                    " ORDER BY startDate DESC " +
                    "</script>"})
    List<Map<String,Object>> feeDataByPerson(Long comId, int pId, int feeItemId,int dataStatus);*/

    @Select({"<script>",
            "SELECT s.id , s.item_id AS fee_item_id , s.data_status , f.item_name ," +
                    "s.plan_money , s.complete_money , " +
                    "s.refund_money  , s.fee_time AS complete_Time, s.refund_time ," +
                    "s.order_no , s.trade_no, s.refund_order_no o, \n" +
                    "f.remark AS item_remark, f.status AS item_status , f.start_date , f.stop_date \n" +
                    "FROM fee_data s LEFT JOIN fee_item f ON f.item_id = s.item_id \n" +
                    "WHERE s.com_id = #{comId} AND s.p_id = #{pId}  AND  f. STATUS > 1 \n" +
                    "<when test=\"dataStatus == 1 \" > "+
                    " AND  s.data_status = 1 \n" +
                    "</when> " +
                    "<when test=\"dataStatus == 4 \" > "+
                    " AND s.data_status IN (4,5,0)\n" +
                    "</when> " +
                    "<when test=\"dataStatus == 5 \" > "+
                    " AND s.data_status IN (5,0)\n" +
                    "</when> " +
                    "<when test=\"feeItemId !=null and feeItemId > 0\" > "+
                    " AND s.item_id = #{feeItemId}\n" +
                    "</when> " +
                    " ORDER BY start_date DESC " +
                    "</script>"})
    List<FeeDataByPersonResponse> feeDataByPerson(Long comId, int pId, int feeItemId, int dataStatus);
}
